<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/8/21
 * Time: 16:04
 */

namespace app\back\controller;
use app\common\model\Category as CategoryModel;
use think\Request;
use think\Session;

class Category extends BaseController
{
    /**
     * 分类列表页
     * @return \think\response\View
     */
    public function index()
    {
        $categories = CategoryModel::getTreeData('tree', 'sort_number');
        $this->assign(compact('categories'));
        return view();
    }

    /**
     * 分类添加页
     * @return \think\response\View
     */
    public function add()
    {
        $categories = CategoryModel::getTreeData('tree', 'sort_number');
        $this->assign(compact('categories'));
        return view();
    }

    /**
     * 分类添加处理
     * @param Request $request
     * @return \think\response\Json
     */
    public function addProcess(Request $request)
    {
        if ($request->isPost()) {
            $model = new CategoryModel();
            $model->pid         = $request->post('pid');
            $model->name        = $request->post('name');
            $model->sort_number = $request->post('sort_number');
            $model->intro       = $request->post('intro');
            $model->user_id     = Session::get('login.id');
            $model->save();
            if ($model->id) {
                return json(['status' => 'success', 'code'  =>  '00000', 'result' => ['id' => $model->id]]);
            }
            return json(['status' => 'failed', 'code'  =>  '10000'], 400);
        }
        return json(['status' => 'failed', 'code'  =>  '10008'], 400);
    }

    /**
     * 分类编辑页
     * @param $id
     * @return \think\response\View
     */
    public function edit($id)
    {
        $model = new CategoryModel();
        $data = $model->find($id)->toArray();
        $categories = $model->getTreeData('tree', 'sort_number');
        return view('edit', compact('categories', 'data'));
    }

    /**
     * 分类编辑处理
     * @param Request $request
     * @return \think\response\Json
     */
    public function editProcess(Request $request)
    {
        if ($request->post('id')) {
            $model = CategoryModel::find($request->post('id'));
            $model->pid         = $request->post('pid');
            $model->name        = $request->post('name');
            $model->sort_number = $request->post('sort_number');
            $model->intro       = $request->post('intro');
            $model->user_id     = Session::get('login.id');;
            $model->save();
            if ($model->id) {
                return json(['status' => 'success', 'code'  =>  '00000', 'result' => ['id' => $model->id]]);
            }
            return json(['status' => 'failed', 'code'  =>  '10000'], 400);
        }
        return json(['status' => 'failed', 'code'  =>  '10008'], 400);
    }

    /**
     * 分类软删除
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        if ($request->post('id')) {
            $model = CategoryModel::get(['pid'=>$request->post('id')]);
            if (!$model) {
                CategoryModel::destroy($request->post('id'));
                return json(['status' => 'success', 'code'  =>  '00000', 'result' => ['id' => $request->post('id')]]);
            }
            return json(['status' => 'failed', 'code'  =>  '10003'], 400);
        }
        return json(['status' => 'failed', 'code'  =>  '10005'], 400);
    }

}